package v1

import (
	"net/http"
	"toy-car/models"
	"toy-car/pkg/e"

	"github.com/gin-gonic/gin"
)

// @Tags Casbin
// @Summary 更新角色api权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CasbinInReceive true "权限id, 权限模型列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /casbin/UpdateCasbin [post]
func CasbinUpdate(c *gin.Context) {
	var cmr models.CasbinInReceive
	_ = c.ShouldBindJSON(&cmr)

	code := e.SUCCESS

	err := models.UpdateCasbin(cmr.AuthorityId, cmr.CasbinInfos)
	if err != nil {
		code = e.ERROR
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": c,
	})
}

// @Tags Casbin
// @Summary 获取权限列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CasbinInReceive true "权限id, 权限模型列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/getPolicyPathByAuthorityId [post]
func CasbinGetByAuthorityID(c *gin.Context) {
	var casbin models.CasbinInReceive
	_ = c.ShouldBindJSON(&casbin)
	code := e.SUCCESS
	paths := models.GetPolicyPathByAuthorityId(casbin.AuthorityId)
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": map[string]interface{}{"Paths": paths},
	})
}
